package com.yimi.yinhepda.db.dao;

import android.database.sqlite.SQLiteDatabase;

import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;
import com.yimi.yinhepda.base.BaseApplication;
import com.yimi.yinhepda.db.helper.DbHelper;
import com.yimi.yinhepda.db.table.QueryQcTypeTable;
import com.yimi.yinhepda.entity.response.QueryQcType;
import com.pda.uce.commom.util.LogUtil;

import java.sql.SQLException;
import java.util.List;

/**
 * @author 000432
 * @date 2017/4/20
 */
public class QueryQcTypeTableDao extends BaseDao<QueryQcTypeTable, Long> {
    private static final String TAG = SortScanTableDao.class.getSimpleName();

    public QueryQcTypeTableDao() {
        super(DbHelper.getInstance(BaseApplication.getContext()), QueryQcTypeTable.class);
    }

    public List<QueryQcTypeTable> findLevel(int typeLevel) {
        try {
            QueryBuilder<QueryQcTypeTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("typeLevel", typeLevel);
            where.and().eq("status", 1);

            List<QueryQcTypeTable> listUnloadScanTable = queryBuilder.query();
            return listUnloadScanTable;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<QueryQcTypeTable> findChildLevel(String superiorTypeCode, int typeLevel) {
        try {
            QueryBuilder<QueryQcTypeTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("typeLevel", typeLevel);
            where.and().eq("superiorTypeCode", superiorTypeCode);
            where.and().eq("status", 1);

            List<QueryQcTypeTable> listUnloadScanTable = queryBuilder.query();
            return listUnloadScanTable;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String findOrderByLatestTime() {
        String lastTime = null;
        try {
            QueryBuilder<QueryQcTypeTable, Long> queryBuilder = dao.queryBuilder().orderBy("latestTime", false).limit(1L);
            List<QueryQcTypeTable> list = queryBuilder.query();
            if (list != null && list.size() > 0) {
                lastTime = list.get(0).getLatestTime();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return lastTime;
    }

    public void createOrUpdate(List<QueryQcType> list) {
        SQLiteDatabase database = getDbHelper().getWritableDatabase();
        String tableName = dao.getTableName();
        StringBuffer sql = new StringBuffer("replace into ");
        sql.append(tableName);
        sql.append("(qcTypeId,");
        sql.append("qcType,");
        sql.append("typeCode,");
        sql.append("typeName,");
        sql.append("superiorTypeCode,");
        sql.append("superiorTypeName,");
        sql.append("status,");
        sql.append("typeLevel,");
        sql.append("isAutoSubmitMistake,");
        sql.append("isAutoSubmitPicture,");
        sql.append("remark,");
        sql.append("dealTerm,");
        sql.append("compCode,");
        sql.append("creater,");
        sql.append("createrTime,");
        sql.append("modifier,");
        sql.append("modifierTime,");
        sql.append("isDelete,");
        sql.append("latestTime,");
        sql.append("recordVersion,");
        sql.append("isNeedToReply,");
        sql.append("replyDepartType,");
        sql.append("isNeedToKnow,");
        sql.append("knowDepartType,");
        sql.append("isAbnormalType,");
        sql.append("isSubmitClaim,");
        sql.append("isMistakeType,");
        sql.append("isAutoChange,");
        sql.append("isAutoPunish,");
        sql.append("isAppealType,");
        sql.append("appealLimit,");
        sql.append("isClaimType,");
        sql.append("typeBelong,");
        sql.append("isRewardpunishType,");
        sql.append("isStowageNo)");

        sql.append("values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
        LogUtil.e("sql=" + sql.toString());

        database.beginTransaction();
        for (QueryQcType item : list) {
            Object[] values = new Object[]{
                    item.getQc_type_id(), item.getQc_type(), item.getType_code(), item.getType_name(), item.getSuperior_type_code(),
                    item.getSuperior_type_name(), item.getStatus(), item.getType_level(), item.getIs_auto_submit_mistake(), item.getIs_auto_submit_pictrue(),
                    item.getRemark(), item.getDeal_term(), item.getComp_code(), item.getCreater(), item.getCreater_time(), item.getModifier(),
                    item.getModifier_time(), item.getIs_delete(), item.getLatest_time(), item.getRecord_version(),item.getIsNeedToReply(),
                    item.getReplyDepartType(),item.getIsNeedToKnow(),item.getKnowDepartType(),item.getIsAbnormalType(),item.getIsSubmitClaim(),
                    item.getIsMistakeType(),item.getIsAutoChange(),item.getIsAutoPunish(),item.getIsAppealType(),item.getAppealLimit(),
                    item.getIsClaimType(),item.getTypeBelong(),item.getIsRewardpunishType(),item.getIsStowageNo()
            };
            database.execSQL(sql.toString(), values);
        }
        database.setTransactionSuccessful();
        database.endTransaction();
    }
}
